package com.example.web_flux.config;

import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.function.HandlerFunction;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.ServerRequest;
import org.springframework.web.servlet.function.ServerResponse;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import static org.springframework.web.servlet.function.RouterFunctions.route;

/**
 * @auth 罗俊华
 * @Date 2022/8/13 8:34 上午
 */
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
//        configurer.setTaskExecutor()
    }

    private final AtomicInteger count = new AtomicInteger(0);

    @Bean
    public RouterFunction<?> hello() {
        return route().GET("/hello", (ServerRequest req) -> {
            return ServerResponse.ok()
                    .header("Content-Type", "application/json;charset=utf-8")

                    .body("我的第一个webFlux程序");
        }).build();
    }

    @Bean
    public RouterFunction<?> pathVariable() {
        return route().GET("/hello/{id}", (ServerRequest req) -> {
            return ServerResponse.ok()
                    .header("Content-Type", "application/json;charset=utf-8")

                    .body("我得到的路径id是：" + req.pathVariable("id"));
        }).filter((ServerRequest request, HandlerFunction<ServerResponse> next) -> {

            if (count.getAndIncrement() % 2 == 0) {
                return ServerResponse.status(HttpStatus.FORBIDDEN).body("您无权访问哦，亲亲");
            }

            return next.handle(request);
        }).build();
    }


    @Override
    public void addFormatters(FormatterRegistry registry) {
        DateFormatter dateFormatter = new DateFormatter("yyyy-MM-dd hh:mm:ss");
        registry.addFormatterForFieldType(Date.class, dateFormatter);

        registry.addFormatter(dateFormatter);
        DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
        registrar.setUseIsoFormat(true);
        registrar.registerFormatters(registry);
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
                .indentOutput(true)
                .dateFormat(new SimpleDateFormat("yyyy-MM-dd"))
                .modulesToInstall(new ParameterNamesModule());
        converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
//        converters.add(new MappingJackson2XmlHttpMessageConverter(builder.createXmlMapper(true).build()));


    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

        registry.addViewController("/jsp")
                .setViewName("hellojsp");

    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("/WEB-INF/jsp/",".jsp");
    }
}
